package com.sls.web.manager.game.service.impl;

import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.YunEnProduct;
import com.sls.web.manager.game.pojo.YunEnRespInfo;
import com.sls.web.manager.game.pojo.wogreat.WogreatProduct;
import com.sls.web.manager.game.service.YunService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

@Service("yunService")
public class YunServiceImpl extends SuperService implements YunService {

	private Gson gson;

	public YunServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public void update(YunEnProduct product, UserInfoVO userInfo) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020983");
		head.setIp(userInfo.getCurrentIp());
		head.setUserId(userInfo.getUserId());
		SocketDTO dto = super.send(head, product);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public YunEnProduct queryById(String id, UserInfoVO userInfo) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020984");
		head.setIp(userInfo.getCurrentIp());
		head.setUserId(userInfo.getUserId());
		SocketDTO dto = super.send(head, id);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				YunEnProduct pro = gson.fromJson(body, YunEnProduct.class);
				return pro;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<YunEnProduct> queryPage(YunEnProduct product,
			UserInfoVO userInfo) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020985");
		head.setIp(userInfo.getCurrentIp());
		head.setUserId(userInfo.getUserId());
		SocketDTO dto = super.send(head, product);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<YunEnProduct> page = gson.fromJson(body,
					new TypeToken<Pagination<YunEnProduct>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void save(YunEnProduct product, UserInfoVO userInfo) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020982");
		head.setIp(userInfo.getCurrentIp());
		head.setUserId(userInfo.getUserId());
		SocketDTO dto = super.send(head, product);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public void confirmAudit(String supplierId, int priceRatio,
			UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020987");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		YunEnProduct yun = new YunEnProduct();
		yun.setSupplierId(supplierId);
		yun.setPriceRatio(priceRatio);
		yun.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, yun);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public void batchOnline(String state, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020988");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		WogreatProduct wp = new WogreatProduct();
		wp.setOperator(userInfoVO.getUserName());
		wp.setStatus(state);
		SocketDTO dto = super.send(head, wp);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public void batchOffline(String state, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020988");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		WogreatProduct wp = new WogreatProduct();
		wp.setOperator(userInfoVO.getUserName());
		wp.setStatus(state);
		SocketDTO dto = super.send(head, wp);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public YunEnRespInfo queryOrder(String orderId, UserInfoVO currentOperator) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021040");
		head.setIp(currentOperator.getCurrentIp());
		head.setUserId(currentOperator.getUserId());
		YunEnRespInfo info = new YunEnRespInfo();
		info.setOrderId(orderId);
		SocketDTO dto = super.send(head, info);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				YunEnRespInfo respInfo = gson.fromJson(body,
						YunEnRespInfo.class);
				return respInfo;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

}
